package com.linln.admin.ws.controller;

import com.linln.admin.ws.domain.Muban;
import com.linln.admin.ws.domain.SendMessageDTO;
import com.linln.admin.ws.domain.WsUser;
import com.linln.admin.ws.service.MubanService;
import com.linln.admin.ws.service.WsUserService;
import com.linln.admin.ws.validator.WsUserValid;
import com.linln.common.enums.StatusEnum;
import com.linln.common.utils.EntityBeanUtil;
import com.linln.common.utils.ResultVoUtil;
import com.linln.common.utils.StatusUtil;
import com.linln.common.vo.ResultVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author sh
 * @date 2023/01/05
 */
@Controller
@RequestMapping("/ws/wsUser")
public class WsUserController {

    @Autowired
    private WsUserService wsUserService;

    @Autowired
    private MubanService mubanService;

    /**
     * 列表页面
     */
    @GetMapping("/index")
    @RequiresPermissions("ws:wsUser:index")
    public String index(Model model, WsUser wsUser) {

        // 创建匹配器，进行动态查询匹配
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withMatcher("wsPhone", match -> match.contains());

        // 获取数据列表
        Example<WsUser> example = Example.of(wsUser, matcher);
        Page<WsUser> list = wsUserService.getPageList(example);

        // 封装数据
        model.addAttribute("list", list.getContent());
        model.addAttribute("page", list);
        return "/ws/wsUser/index";
    }

    /**
     * 跳转到添加页面
     */
    @GetMapping("/add")
    @RequiresPermissions("ws:wsUser:add")
    public String toAdd() {
        return "/ws/wsUser/add";
    }

    /**
     * 跳转到编辑页面
     */
    @GetMapping("/edit/{id}")
    @RequiresPermissions("ws:wsUser:edit")
    public String toEdit(@PathVariable("id") WsUser wsUser, Model model) {
        model.addAttribute("wsUser", wsUser);
        return "/ws/wsUser/add";
    }

    /**
     * 保存添加/修改的数据
     * @param valid 验证对象
     */
    @PostMapping("/save")
    @RequiresPermissions({"ws:wsUser:add", "ws:wsUser:edit"})
    @ResponseBody
    public ResultVo save(@Validated WsUserValid valid, WsUser wsUser) {
        // 复制保留无需修改的数据
        if (wsUser.getId() != null) {
            WsUser beWsUser = wsUserService.getById(wsUser.getId());
            EntityBeanUtil.copyProperties(beWsUser, wsUser);
        }

        // 保存数据
        wsUserService.save(wsUser);
        return ResultVoUtil.SAVE_SUCCESS;
    }

    /**
     * 跳转到详细页面
     */
    @GetMapping("/detail/{id}")
    @RequiresPermissions("ws:wsUser:detail")
    public String toDetail(@PathVariable("id") WsUser wsUser, Model model) {
        model.addAttribute("wsUser",wsUser);
        return "/ws/wsUser/detail";
    }

    /**
     * 设置一条或者多条数据的状态
     */
    @RequestMapping("/status/{param}")
    @RequiresPermissions("ws:wsUser:status")
    @ResponseBody
    public ResultVo status(
            @PathVariable("param") String param,
            @RequestParam(value = "ids", required = false) List<Long> ids) {
        // 更新状态
        StatusEnum statusEnum = StatusUtil.getStatusEnum(param);
        if (wsUserService.updateStatus(statusEnum, ids)) {
            return ResultVoUtil.success(statusEnum.getMessage() + "成功");
        } else {
            return ResultVoUtil.error(statusEnum.getMessage() + "失败，请重新操作");
        }
    }



    @GetMapping("/getWhatAppMessage")
    public String getMessage(
            @RequestParam(value = "ids", required = false) List<Long> ids , Model model) {
        //获取所有的模板
        List<Muban> mubanList = mubanService.getAllMuBan();
        // 实用模板id发送消息
        model.addAttribute("ids",ids);
        model.addAttribute("mubanList",mubanList);

        return "/ws/wsUser/sendMessage";
    }


    /**
     * 设置一条或者多条数据的状态
     */
    @RequestMapping("/sendMessage")
    @RequiresPermissions("ws:wsUser:status")
    @ResponseBody
    public ResultVo sendMessage(
            SendMessageDTO sendMessageDTO) {
        // 更新状态
        // 实用模板id发送消息
        wsUserService.sendMessage(sendMessageDTO);
        return ResultVoUtil.success("正在后台发送消息中");
    }
}